﻿if (typeof Axis == "undefined") Axis = {};
if (typeof Axis.View == "undefined") Axis.View = {};
if (typeof Axis.View.Member == "undefined") Axis.View.Member = {};
Axis.View.Member.Profile = {
    Init: function () {
        var thisObj = this;
        $("button").button();
        thisObj.Event();
        thisObj.GetMemberInfo();
        thisObj.MaskDate.Init();
        thisObj.AvatarUpload.Init();
    },
    AvatarUpload: {
        Instant: null,
        Init: function () {
            $('#Avatar_Upload').uploadify({
                multi: false,
                width: 185,
                buttonText: 'Thay đổi hình đại diện',
                fileTypeDesc: 'Image Files',
                fileTypeExts: '*.gif; *.jpg; *.jpeg, *.bmp; *.png',
                fileSizeLimit: 500,
                onUploadSuccess: function (file, data, response) {
                    if (data) {
                        data = eval("(" + data + ")");
                        if (data.IsSuccessfull) {
                            $("#txtAvatar").val(data.FilePath);
                            $("#imgLoggedAvatar,#imgAvatar").attr("src", data.FilePath + "?Guid=" + $.GuidGenerator());
                        }
                    }
                }
            });
        }
    },
    MaskDate: {
        Instant: null,
        Init: function () {
            this.Instant = new CMaskDate();
            this.Instant.Init($("input[role=datepicker]"));
        }
    },
    CheckInputNumber: function () {
        $("#txtUserCMND,#txtUserMobile,#txtUserPhone").keydown(function (e) {
            var key = e.charCode || e.keyCode || 0;
            return (
                       key == 8
                    || key == 9
                    || key == 46
                    || (key >= 35 && key <= 40)
                    || (key >= 48 && key <= 57 && event.shiftKey == false)
                    || (key >= 96 && key <= 105)
                );
        });
    },
    GetMemberInfo: function () {
        var thisObj = this;
        $.Ajax({
            url: "Service/AccountService.asmx/GetMember",
            type: 'POST',
            data: { pInputValue: "<InputValue />" },
            beforeSend: function () {
            },
            success: function (data, textStatus, xhr) {
                if (data) {
                    data = $.findInXml(data);
                    data = eval("(" + data + ")");
                    data = data[0];
                    $.each(data, function (key, value) {
                        if (value && value.toString().indexOf("/Date") != -1) {
                            value = new Date(parseInt(value.replace("/Date(", "").replace(")/", ""), 10));
                            value = $.CFormat.FormatDate(value, "d", "vi-VN");
                        }
                        if (key == "Avatar") {
                            $("#imgAvatar").attr("src", value);
                        }
                        $("[data-field=" + key + "]").val(value);
                    });

                    $('#txtFullName').val(data.FirstName + " " + data.LastName);
                    $(".total-mark-value").text($.CFormat.FormatNumber(data.Mark, "n0", "vi-VN"));
                }
            },
            error: function (xhr, textStatus, errorThrown) { }
        });
    },
    Update: function (pData, callback) {
        var thisObj = this;
        var overlay = new COverlay({
            autoShow: false
        });
        overlay.Init();
        $.Ajax({
            url: "Service/UpdateMember.ashx",
            type: 'POST',
            data: pData,
            beforeSend: function () {
            },
            success: function (data, textStatus, xhr) {
                if (data) {
                    data = eval("(" + data + ")");
                    var IsSuccessfull = data.IsSuccessfull;
                    $.CAlert.Alert({
                        Title: "Cập nhật tài khoản",
                        Content: data.Description,
                        ButtonText: "OK",
                        dialogClass: "Alert",
                        width: 550,
                        height: 'auto'
                    });
                    if (typeof callback == "function") {
                        callback(data);
                    }
                }
            },
            beforeSend: function () {
                overlay.Show();
            },
            complete: function () {
                overlay.Hide();
            },
            error: function (xhr, textStatus, errorThrown) {
                overlay.hide();
            }
        });
    },
    Mark2Gift: {
        Instant: null,
        Init: function () {
            var thisObj = this;
            thisObj.Instant = new CDialog({
                title: "● ĐỔI ĐIỂM LẤY QUÀ ●",
                width: 610,
                dialogClass: "Mark2Gift",
                closeHide: true,
                buttons: [
                    {
                        text: "OK",
                        click: function () {
                            thisObj.Instant.Close();
                        }
                    }
                ],
            });
            thisObj.Instant.Init("#Mark2GiftDialog");
            $(".Mark2GiftDialog_Gifts").carouFredSel({
                circular: true,
                items: 4,
                auto: {
                    play: false
                },
                prev: {
                    button: "#Mark2GiftDialog_Gift_prev",
                    key: "left"
                },
                next: {
                    button: "#Mark2GiftDialog_Gift_next",
                    key: "right"
                }
            });

        }
    },
    Event: function () {
        var thisObj = this;
        thisObj.CheckInputNumber();
        $("#btnUpdate").click(function () {
            var cvalidator = new CValidator({
                zIndex: 110
            });
            cvalidator.Init("#UsersGridEdit :input");
            if (!cvalidator.CheckValidity())
                return;
            var data = {};
            $("[data-field]").each(function () {
                var $this = $(this);
                var key = $this.attr("data-field");
                var value = $this.val();
                if ($this.attr("role") == "datepicker") {
                    value = $.CFormat.FormatDate(value, "yyyy/MM/dd", "vi-VN", "en-US");
                }
                var dataRole = $this.attr("data-role") || "";
                if (dataRole != "noupdate") {
                    data[key] = value;
                }
            });
            data.Action = "UPDATE";
            thisObj.Update(data);
        });
        $("#link_ChangePassword").click(function () {
            var isCancel = $(this).attr("isCancel") || "0";
            if (isCancel == "0") {
                $("#txt_Password").removeAttr("readonly");
                $("div[data-role='change_password']").show();
                $("#txt_PasswordNew,#txt_PasswordConfirm").val("");
                $("#txt_Password").val("").focus();
                $(this).text("Bỏ qua").attr("isCancel", 1);

            }
            else {
                $("#txt_Password").val("1234567890").attr("readonly", "readonly");
                $("div[data-role='change_password']").hide();
                $(this).text("Đổi mật khẩu").attr("isCancel", 0);
            }
        });

        $("#link_ChangeName").click(function () {
            $('.divFullName').hide();
            $('.divName').show();
        });
        $("#btnMoreInfo").click(function () {
            $('.moreInfo').show();
            $(this).hide();
        });
        $("#link_Complete").click(function () {
            $("#btnUpdate").click();
        });
        $("#btn_ChangeAvatar").click(function () {
            $("#file-uploader-avatar").find("input[type='file']").trigger("click");
        });
        $("#btn_ChangePassword").click(function () {
            var data = {
                ID: $("#txtUserID").val(),
                Action: "CHANGEPASS",
                OldPassword: $("#txt_Password").val(),
                NewPassword: $("#txt_PasswordNew").val()

            };
            thisObj.Update(data, function (_data) {
                if (_data && _data.IsSuccessfull) {
                    $("#link_ChangePassword").trigger("click");
                }
            });
        });
        $("#imgMark2Gift").click(function () {
            thisObj.Mark2Gift.Init();
        });

        $(".Mark2GiftDialog_Gift").live("click", function () {
            $(".Mark2GiftDialog_Gifts").find(".Mark2GiftDialog_Gift").removeClass("Mark2GiftDialog_Gift_Selected");
            $(this).addClass("Mark2GiftDialog_Gift_Selected");
        });
    },
      ChangeMarkToGift:function(ID)
    {
         var thisObj = this;
        var pData = {ID:ID};
        $.Ajax({
            url: 'Service/AccountService.asmx/ChangeMarkToGift',
            type: 'POST',
             data: pData,
             success: function (data, textStatus, jqXHR) {
                    if (data) {
                        data = $.findInXml(data);
                        data = eval("(" + data + ")");
                        $.CAlert.Alert({
                        Title: data.Name,
                        Content: data.Description,
                        ButtonText: "OK",
                        dialogClass: "Alert",
                        width: 550,
                        height: 'auto'
                    });
                    
                        }
            },
            beforeSend: function (jqXHR, settings) {
            },
            error: function (jqXHR, textStatus, errorThrown) {
            },
            complete: function (XMLHttpRequest, textStatus) {
            }
        });
    }
};
$(function () {
    Axis.View.Member.Profile.Init();
});

function ChangeMarkToGift(ID){
$.CAlert.Confirm({
                            Title: "Đồi điểm lấy quà",
                            Content: $.string.Format("Bạn muốn đổi điểm lấy quà.")
                        }, function (isOK) {
                            if (isOK) {
                            Axis.View.Member.Profile.ChangeMarkToGift(ID);
                            }
                        });
}